/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package beans;

import Utility.Utility;
import java.io.Serializable;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import xmlMapping.Member;

/**
 *
 * @author root
 */
@ManagedBean(name = "login")
@RequestScoped
public class LoginBean implements Serializable {

    private String userName;
    private String password;

    /** Creates a new instance of LoginBean */
    public LoginBean() {
    }

    /**
     * @return the username
     */
    public String getUserName() {
        return userName;
    }

    /**
     * @return the password
     */
    public String getPassword() {
        return password;
    }

    /**
     * @param userName the userName to set
     */
    public void setUserName(String userName) {
        this.userName = userName;
    }

    /**
     * @param password the password to set
     */
    public void setPassword(String password) {
        this.password = password;
    }

    // this method to check login 
    public String loginUser() {
        String result = "";
        Member check = new Member();
        System.out.println("in high level user name is  " + getUserName() + "pasword is " + getPassword());
        check.setUserName(getUserName());
        check.setPassword(getPassword());
        Utility utility = new Utility();
        Member newMember = utility.login(check);
    
        
        if (newMember != null) {
        

            result = "home";

        } else {
       
            FacesContext facesContext = FacesContext.getCurrentInstance();
            FacesMessage facesMessage = new FacesMessage(
                    "You have entered an invalid user name and/or password");
            facesContext.addMessage("username", facesMessage);

            result = "Login";
        }

        return result;
    }
}
